package de.awg.accountanalyzer.entities;

import java.util.HashSet;
import java.util.Set;

import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleSetProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableSet;

public class Account extends ChartEntity implements Comparable<Account> {
	private SimpleStringProperty accountName;
	private SimpleSetProperty<AccountPosition> accountPositionSet;
	private SimpleBooleanProperty visible = new SimpleBooleanProperty(true);

	public Account(String pAccountName) {
		accountName = new SimpleStringProperty();
		setAccountName(pAccountName);
		accountPositionSet = new SimpleSetProperty<>(FXCollections.observableSet(new HashSet<AccountPosition>()));
	}

	public String getAccountName() {
		return accountName.get();
	}

	public void setAccountName(String accountName) {
		this.accountName.set(accountName);
	}

	// hold javafx beans contract by providing this method
	public SimpleStringProperty accountNameProperty() {
		return accountName;
	}

	public Set<AccountPosition> getAccountPositionSet() {
		return accountPositionSet.get();
	}

	public void setAccountPositionList(ObservableSet<AccountPosition> accountPositionList) {
		this.accountPositionSet.set(accountPositionList);
	}

	// hold javafx beans contract by providing this method
	public SimpleSetProperty<AccountPosition> accountPositionSetProperty() {
		return accountPositionSet;
	}

	public void addPosition(AccountPosition ap) {
		accountPositionSet.add(ap);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((accountName == null) ? 0 : accountName.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Account other = (Account) obj;
		if (accountName == null) {
			if (other.accountName != null)
				return false;
		} else if (!accountName.equals(other.accountName))
			return false;
		if (accountPositionSet == null) {
			if (other.accountPositionSet != null)
				return false;
		} else if (!accountPositionSet.equals(other.accountPositionSet))
			return false;
		return true;
	}

	@Override
	public String toString() {
		StringBuffer buffer = new StringBuffer();
		buffer.append("Account: ");
		buffer.append(this.getAccountName());
		buffer.append("\n");

		for (AccountPosition position : accountPositionSet) {
			buffer.append(position);
			buffer.append("\n");
		}

		return buffer.toString();
	}

	@Override
	public int compareTo(Account o) {
		return this.getAccountName().compareTo(o.getAccountName());
	}

	@Override
	public double getValue() {
		double result = 0;
		for (AccountPosition position : accountPositionSet) {
			result += position.getValue();
		}
		return result;
	}

	@Override
	public String getName() {
		return this.accountName.get();
	}

	public boolean getVisible() {
		return visible.get();
	}

	public void setVisible(boolean visible) {
		this.visible.set(visible);
		//reiche den neuen Wert durch bis zur jeder AccountPosition
		for (AccountPosition accountPosition : accountPositionSet) {
			accountPosition.setVisible(visible);
		}
	}

	// hold javafx beans contract by providing this method
	public SimpleBooleanProperty visibleProperty() {
		return visible;
	}
}
